/*---------------------------------------------------------------------------------

A very simple spotlight demo to show background windowing.

---------------------------------------------------------------------------------*/
#include <nds.h>
#include "drunkenlogo.h"

//global 
s16 center = SCREEN_WIDTH / 2;

//{b=hblankHandler}
void hblankIrq(void)
{
	u8 delta = (REG_VCOUNT + 40) / 3;
	
	s16 left =  (center - delta);
	s16 right = (center + delta);

	if(left < 0) left = 0;
	if(right > SCREEN_WIDTH - 1) right = SCREEN_WIDTH - 1;
	 
	windowSetBounds(WINDOW_0, left, 0, right, SCREEN_HEIGHT - 1);
}
//{b=hblankHandler}

//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	//{b=vidInit}
	videoSetMode(MODE_5_2D);
	vramSetBankA(VRAM_A_MAIN_BG);
   
	int bg3 = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0,0);
	
	//use the standard drunken logo
	dmaCopy(drunkenlogoBitmap, bgGetGfxPtr(bg3), drunkenlogoBitmapLen);
	dmaCopy(drunkenlogoPal, BG_PALETTE, drunkenlogoPalLen);
    //{b=vidInit}
   
	//setup and enable the window registers: {b=winInit}
	windowEnable(WINDOW_0);
	bgWindowEnable(bg3, WINDOW_0);
    //{b=winInit}
    
	//setup and enable the hblank irq {b=irq}
	irqSet(IRQ_HBLANK, hblankIrq);
	irqEnable(IRQ_HBLANK);
    //{b=irq}
    
    //{b=mainLoop}
	while(1) 
	{
		scanKeys();

		if(keysHeld() & KEY_LEFT) center--;
		if(keysHeld() & KEY_RIGHT) center++;

		if(center < 0) center = 0;
		if(center > SCREEN_WIDTH - 1) center = SCREEN_WIDTH - 1;

		swiWaitForVBlank();
	}
    //{b=mainLoop}
}
